package main

import (
	base "2024-fall-etohlcin3-backend/cmd/kitex_gen/industics/blockchain/base"
	"context"

	"github.com/hyperledger/fabric-gateway/pkg/client"
)

// DesignServiceImpl implements the last service interface defined in the IDL.
type DesignServiceImpl struct{}

var DesignOrg *client.Contract

// GetBOM implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) GetBOM(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())

	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}

	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetECN implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) GetECN(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())

	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}

	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetBOMHistory implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) GetBOMHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetECNHistory implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) GetECNHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetBOMByRange implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) GetBOMByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetECNByRange implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) GetECNByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// CreateBOM implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) CreateBOM(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// CreateECN implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) CreateECN(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// UpdateBOM implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) UpdateBOM(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// UpdateECN implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) UpdateECN(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeleteBOM implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) DeleteBOM(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeleteECN implements the DesignServiceImpl interface.
func (s *DesignServiceImpl) DeleteECN(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

func readForKey(formID string) ([]byte, error) {
	return DesignOrg.EvaluateTransaction("ReadForm", formID)
}
func readHistoryForKey(formID string) ([]byte, error) {
	return DesignOrg.EvaluateTransaction("ReadFormHistoryForKey", formID)
}
func readForRange(formIDStart string, formIDEnd string) ([]byte, error) {
	return DesignOrg.EvaluateTransaction("ReadFormsByRange", formIDStart, formIDEnd)
}
func createForKey(formID string, formJSON string) ([]byte, error) {
	return DesignOrg.SubmitTransaction("CreateForm", formID, formJSON)
}
func updateForKey(formID string, formJSON string) ([]byte, error) {
	return DesignOrg.SubmitTransaction("UpdateForm", formID, formJSON)
}
func deleteForKey(formID string) ([]byte, error) {
	return DesignOrg.SubmitTransaction("DeleteForm", formID)
}
